package org.csu.jpetstore.util;


import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Random;

/**
 * @author huangzhangyan
 */
public class CodeUtil {
    /**
     * 存放在内存中的验证码Map
     */
    private static final HashMap<String, String> CODE_MAP = new HashMap<>();
    private static final String ALL_CODE_CHAR = "ABCDEFGHGKMNPQRSTUVWXY3456789";
    /**
     * 全局的验证码Index
     */
    private static int codeKeyIndex = 0;

    private synchronized static String getCodeKey() {
        return Integer.toString(codeKeyIndex++);
    }

    /**
     * 校验验证码
     *
     * @param codeKey   验证码ID
     * @param codeValue 验证码值
     * @return boolean
     * @author huangzhangyan
     * @date 2022/4/4 1:13
     */
    public static boolean verifyCode(String codeKey, String codeValue) {
        return CODE_MAP.get(codeKey).equalsIgnoreCase(codeValue);
    }

    /**
     * 获取验证码ID,值,图片
     *
     * @return Triple<String, String, Graphics> 分别为 codeKey验证码ID, codeValue验证码值, graphics验证码图片
     * @author huangzhangyan
     * @date 2022/4/4 0:25
     */
    private static Triple<String, String, Graphics> getCode() {
        String codeKey = getCodeKey();
        String codeValue = getRanString(4);
        Graphics graphics = createCodeImage(codeValue);
        //保存在codeMap中
        CODE_MAP.put(codeKey, codeValue);
        return new ImmutableTriple<>(codeKey, codeValue, graphics);
    }

    /**
     * 获取验证码ID,图片
     *
     * @return Triple<String, Graphics> 分别为 codeKey验证码ID, graphics验证码图片
     * @author huangzhangyan
     * @date 2022/4/4 0:25
     */
    public static Pair<String, Graphics> getCodeKeyAndGraphics() {
        Triple<String, String, Graphics> triple = getCode();
        return new ImmutablePair<>(triple.getLeft(), triple.getRight());
    }

    private static String getRanString(int codeSize) {

        StringBuilder number = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < codeSize; i++) {
            char c = ALL_CODE_CHAR.charAt(r.nextInt(ALL_CODE_CHAR.length()));
            number.append(c);
        }
        return number.toString();
    }

    private static Graphics createCodeImage(String codeValue) {
        //创建空白图片
        BufferedImage image = new BufferedImage(100, 30, BufferedImage.TYPE_INT_RGB);
        //获取图片画笔
        Graphics codeGraphics = image.getGraphics();
        Random r = new Random();
        //设置画笔颜色
        codeGraphics.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
        //绘制矩形背景
        codeGraphics.fillRect(0, 0, 100, 30);
        //绘制干扰线
        //干扰线数量
        int lineNum = 20;
        for (int i = 0; i < lineNum; i++) {
            codeGraphics.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
            codeGraphics.drawLine(r.nextInt(100), r.nextInt(30), r.nextInt(100), r.nextInt(30));
        }
        //设置画笔颜色
        codeGraphics.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));

        //绘制codeValue字符
        codeGraphics.setFont(new Font(null, Font.BOLD + Font.ITALIC, 24));
        codeGraphics.drawString(codeValue, 15, 25);
        return codeGraphics;
    }
}
